/*
 * Copyright 北京航空航天大学 @ 2015 版权所有
 */
package com.buaa.edu.leetcode.algorithm.string;

/**
 * <p>
 * 字符串的反转 
 * Given s = "the sky is blue", 
 * return "blue is sky the".
 * </p>
 * 
 * @author towan
 * @email tongwenzide@163.com 2015年5月19日
 */
public class ReverseWordsInString {

    public String reverseWords(String s) {
        String[] strs = s.split(" ");
        StringBuffer sb = new StringBuffer();
        for(int i=strs.length-1;i>=0;i--){
            //去除空格
            if(" ".endsWith(strs[i])){
                continue;
            }
            sb.append(strs[i]+" ");
        }
        return sb.toString().trim();
    }
    public static void main(String[] args) {
        //
        //"   a   b "
        ReverseWordsInString wordsInString = new ReverseWordsInString();
        String s = "the sky is blue";
        String reverseWords = wordsInString.reverseWords(s );
        System.out.println(reverseWords);
        System.out.println("blue is sky the".endsWith(reverseWords));
    }
    public static void main2(String[] args) {
        //
        //
        ReverseWordsInString wordsInString = new ReverseWordsInString();
        String s = "   a   b ";
        String reverseWords = wordsInString.reverseWords(s );
        System.out.println(reverseWords);
        System.out.println("b a".endsWith(reverseWords));
    }
}
